package com.business.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.domain.Activities;
import com.business.domain.ActivitiesCar;
import com.business.domain.ActivitiesLoan;
import com.business.domain.CarBrand;
import com.business.domain.CarModelVo;
import com.business.domain.Loan;
import com.business.service.ActivitiesService;
import com.business.service.CarService;
import com.business.service.LoanService;
import com.sys.domain.AdminProvince;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;



@Controller
@RequestMapping("/admin/activities")
public class ActivitiesController {

	@Resource
	private ActivitiesService activitiesService;
	
	@Resource
	private LoanService loanService;
	
	@Resource
	private CarService carService;
	
	//到活动列表页面
	@RequestMapping("/actPage")
	public String toActivitiestPage(Model model, @RequestParam("title") String title){
		model.addAttribute("title", title);
		return "activities/actPage";
	}
	
	//查询活动列表
	@RequestMapping("/queryActivitiesList")
	public @ResponseBody RequestPageVo<Activities> queryActivitiesList(PageParam param,String type){
		RequestPageVo<Activities> pageVo= activitiesService.queryActivitiesList(param,type);
		return pageVo;
	}
	
	//删除活动
	@RequestMapping("/deleteActivities")
	public @ResponseBody RequestResultVo<Activities> deleteActivities(Integer id){
		RequestResultVo<Activities> result = new RequestResultVo<Activities>();
		result = activitiesService.deleteActivities(id);
		return result;
	}
	
	//添加或修改活动
	@RequestMapping("/updateActivities")
	public @ResponseBody RequestResultVo<Activities> updateActivities(Activities act){
		RequestResultVo<Activities> result = new RequestResultVo<Activities>();
		if(act.getId()!=null){//
			result=activitiesService.updateActivities(act);
		}else{
			result=activitiesService.addActivities(act);
		}
		return result;
	}
	
	//到车型活动列表页面
	@RequestMapping("/toActCarPage")
	public String toActCarPage(Model model, @RequestParam("title") String title){
		model.addAttribute("title", title);
		
		//查询所有优惠活动类型 用于下拉框选择
		List<Activities> activitiesList=activitiesService.queryAllActivities();
		model.addAttribute("activitiesList", activitiesList);
		
		//查询所有品牌 用于下拉框选择
		List<CarBrand> carBrandList=carService.getAllCarBrand();
		model.addAttribute("carBrandList", carBrandList);
		
		//查询所有地区 用于下拉框选择
		List<AdminProvince> provinceList=activitiesService.queryProvinceList();
		model.addAttribute("provinceList", provinceList);

		return "activities/actCarPage";
	}
	
	@RequestMapping("/getSeriesListBySeriesId")
	public @ResponseBody RequestPageVo<CarModelVo> getSeriesListByBrandId(Integer seriesId){
		
		return carService.getModelListBySeriesId(seriesId);
	}
	
	//查询车型活动列表
	@RequestMapping("/queryActCarList")
	public @ResponseBody RequestPageVo<ActivitiesCar> queryActCarList(PageParam param,String activitiesType){
		RequestPageVo<ActivitiesCar> pageVo= activitiesService.queryActivitiesCarList(param,activitiesType);
		return pageVo;
	}
	
	//添加或修改车型活动
	@RequestMapping("/saveOrUpdateActivitiesCar")
	public @ResponseBody RequestResultVo<ActivitiesCar> saveOrUpdateActivitiesCar(ActivitiesCar act){
		RequestResultVo<ActivitiesCar> result = new RequestResultVo<ActivitiesCar>();
		if (activitiesService.checkActiceCar(act)) {
			if (act.getId() != null) {//
				result = activitiesService.updateActivitiesCar(act);
			} else {
				result = activitiesService.addActivitiesCar(act);
			}
		}else{
			result.setSuccess(false);
			result.setErrorMessage("此时间段已经有优惠活动，请查询后在试");
		}
		return result;
	}
	
	//删除车型活动
	@RequestMapping("/deleteActivitiesCar")
	public @ResponseBody RequestResultVo<ActivitiesCar> deleteActivitiesCar(Integer id){
		RequestResultVo<ActivitiesCar> result = new RequestResultVo<ActivitiesCar>();
		result = activitiesService.deleteActivitiesCar(id);
		return result;
	}
		
	//到车贷活动列表页面
	@RequestMapping("/toActLoanPage")
	public String toActLoanPage(Model model, @RequestParam("title") String title){
		model.addAttribute("title", title);
		
		//查询所有车贷产品 用于下拉框选择
		List<Loan> loanList=loanService.queryAllLoan();
		model.addAttribute("loanList", loanList);
		
		//查询所有优惠活动类型 用于下拉框选择
		List<Activities> activitiesList=activitiesService.queryAllActivities();
		model.addAttribute("activitiesList", activitiesList);
		
		return "activities/actLoanPage";
	}
	
	//查询车贷活动列表
	@RequestMapping("/queryActLoanList")
	public @ResponseBody RequestPageVo<ActivitiesLoan> queryActLoanList(PageParam param,String activitiesType){
		RequestPageVo<ActivitiesLoan> pageVo= activitiesService.queryActivitiesLoanList(param,activitiesType);
		return pageVo;
	}
	
	//添加或修改车贷活动
	@RequestMapping("/saveOrUpdateActivitiesLoan")
	public @ResponseBody RequestResultVo<ActivitiesLoan> saveOrUpdateActivitiesLoan(ActivitiesLoan act){
		RequestResultVo<ActivitiesLoan> result = new RequestResultVo<ActivitiesLoan>();
		if(activitiesService.checkLoan(act)){
			if(act.getId()!=null){//
				result=activitiesService.updateActivitiesLoan(act);
			}else{
				result=activitiesService.addActivitiesLoan(act);
			}
		}else{
			result.setSuccess(false);
			result.setErrorMessage("此时间段已经有优惠活动，请查询后在试");
		}
		return result;
	}
	
	//删除车贷活动
	@RequestMapping("/deleteActivitiesLoan")
	public @ResponseBody RequestResultVo<ActivitiesLoan> deleteActivitiesLoan(Integer id){
		RequestResultVo<ActivitiesLoan> result = new RequestResultVo<ActivitiesLoan>();
		result = activitiesService.deleteActivitiesLoan(id);
		return result;
	}
}
